import Joi from 'joi';
import * as validators from './index';
import joiOmit from '../../server/helpers/joiOmit';
import joiToFilterArg from '../../server/helpers/joiToFilterArg';

export function builder(validators) {
	return Object.entries(validators).reduce((obj, [key, validator]) => {
		if (!key.endsWith('WithId')) return obj;
		const name = key.replace(/WithId$/, '');
		obj[name] = joiToFilterArg(validator)
			.keys({
				OR: Joi.array().items(joiOmit(validator)),
				AND: Joi.array().items(joiOmit(validator)),
			})
			.description('过滤条件');
		return obj;
	}, {});
}

export default builder(validators);
